﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFDataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataBase mydatabase = new DataBase();
        public DataSet ds = new DataSet();
        public DataTable dt=new DataTable();
        public void updatData()
        {
            String sql = "select * from student";
            if (mydatabase.doUpdate(ds, sql, dt.TableName))
            {
                MessageBox.Show("数据更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("数据更新失败，请检查数据是否合法！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ds = mydatabase.GetDataSet("select * from student");
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt.DefaultView;
        }

        private void btnUpdata_Click(object sender, EventArgs e)
        {
            updatData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("数据删除后将不可恢复，真的要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                dataGridView1.Refresh();
                updatData();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            dataGridView1.Rows[i].Selected = false;
            dataGridView1.Rows[0].Selected = true;
            showmess();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i=dataGridView1.SelectedRows[0].Index;
            if(i<dataGridView1.Rows.Count -1)
            {
                dataGridView1.Rows[i].Selected = false;
                dataGridView1.Rows[i+1].Selected=true;
                showmess();
            }
            else
            {
                MessageBox.Show("已经是最后一项了！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i=dataGridView1.SelectedRows[0].Index;
            if(i!=0)
            {
                dataGridView1.Rows[i].Selected = false;
                dataGridView1.Rows[i - 1].Selected = true;
                showmess();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int i= dataGridView1.SelectedRows[0].Index;
            dataGridView1.Rows[i].Selected=false;
            dataGridView1.Rows[dataGridView1.Rows.Count -1].Selected=true;
            showmess();
        }
         private void showmess()
         {
             toolStripStatusLabel1.Text = string.Format("当前是第{0}条记录，共{1}条记录", 
                                        dataGridView1.SelectedRows[0].Index + 1,dataGridView1.Rows.Count);
             toolStripProgressBar1.Value = (int)((double)(dataGridView1.SelectedRows[0].Index) / dataGridView1.Rows.Count * 100);
        }

         private void button7_Click(object sender, EventArgs e)
         {
             Application.Exit();
         }
    }
}
